

import java.io.IOException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Servlet implementation class UTF
 */
@WebServlet("/utf")
public class UTF extends HttpServlet {
	private static final long serialVersionUID = 1L;

    /**
     * Default constructor. 
     */
    public UTF() {
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String method = request.getParameter("method");
		Controller controller = Controller.getInstance();
		
		switch (method) {
		case "putFlag": {
			
			Integer userId = Integer.parseInt(request.getParameter("userId"));
			String str =  request.getParameter("coordinates");
			Integer cityId  = Integer.parseInt(request.getParameter("cityId"));
			String coordinates = str.substring(0, str.length()-1);
			String index = request.getParameter("index");
			
			
			if( controller.isHomeBase(userId))
			{
				Integer coordinateId = controller.putFlag(userId, coordinates,cityId , index, 5);
				controller.createHomeBase( userId, coordinates,cityId, coordinateId);
				Output out = new Output(response);
				CoordResponse coord = new CoordResponse(coordinateId, true);
				out.format(coord);
				
				
				
			}
			else
			{
				
				Integer coordId = controller.putFlag(userId, coordinates,cityId , index, 0);
				Output out = new Output(response);
				CoordResponse coord = new CoordResponse(coordId, false);
				out.format(coord);
				
				
			}
			
			
		}
		break;
		
		
		
		case "getCoordinates": {

			Integer userId = Integer.parseInt(request.getParameter("userId"));
			Coordinates coordinates = controller.getCoordinates(userId);
			Output out = new Output(response);
			out.format(coordinates);
			
		}
		break;
		
		case "getCity": {

			String cityName = request.getParameter("city");
			City city = controller.getCity(cityName);
			Output out = new Output(response);
			out.format(city);
		}
		break;
		
		case "isInsideHomebase":
		{
			String coordinate = request.getParameter("homebase");
			Integer userId = Integer.parseInt(request.getParameter("userId"));
			Integer city = Integer.parseInt(request.getParameter("cityId"));
			
			boolean quest = controller.isInsideHomebase(coordinate,userId, city );
			Output out = new Output(response);
			out.format(quest);
			
		}
		
		
		case "getSquareId":
		{
			String coordinate = request.getParameter("coordinate");
			City city = controller.getCityByCoordinate(coordinate);
			Output out = new Output(response);
			out.format(city);
			
		}
		
		
		case "getUserData": {

			Integer userId = Integer.parseInt(request.getParameter("userId"));
			User user = controller.getUser(userId);
			user.setPassword("");
			Output out = new Output(response);
			out.format(user);
			
		}
		break;
		
		case"getUsersFromCity":
		{
			Integer cityId = Integer.parseInt(request.getParameter("cityId"));
			List <User> users = controller.getUsersFromCity(cityId);
			Output out = new Output(response);
			out.format(users);
			
			
			
		}
		
		case "checkinHomeBase": {
			Integer cityid = Integer.parseInt(request.getParameter("cityId"));
			Integer userId = Integer.parseInt(request.getParameter("userId"));
			Homebase home =  controller.checkinHomeBase(cityid, userId);
			Output out = new Output(response);
			out.format(home);
			
		}
		break;
		case "test": {
			Integer param = Integer.parseInt(request.getParameter("param"));
			Output out = new Output(response);
			out.format(param);
			
		}
		break;
		
		case "passwordCheck": {

			Integer uid = Integer.parseInt(request.getParameter("userId"));
			String oldPass = request.getParameter("oldpass");
			boolean check = controller.passwordCheck(uid, oldPass);
			Output out = new Output(response);
			out.format(check);
			
		}
		break;
		
		
		case "checkTime": {

			Integer homebaseId = Integer.parseInt(request.getParameter("homebase"));
			Homebase homebase = controller.checkHomebase(homebaseId);
					
			
			Output out = new Output(response);
			out.format(homebase);
			
		}
		break;
		
		
		
		}
	}

	
	
	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String method = request.getParameter("method");
		Controller controller = Controller.getInstance();
		
		switch (method) {
		case "register": {
				
			User user = new User(request);
			controller.register(user);
			Output out = new Output(response);
			out.format(user);
		}
		break;
		
		case "login": {
			
			String login =  request.getParameter("login");
			String password =  request.getParameter("password");
			User user = controller.getUser(login, password);
			controller.login(user);
			Output out = new Output(response);
			out.format(user);
				
		}
		break;
		
		case "updateCoins": {
			
			Integer  originCoord =  Integer.parseInt(request.getParameter("orign"));
			Integer  targetCoord =  Integer.parseInt(request.getParameter("target"));
			Integer  coin = 		Integer.parseInt(request.getParameter("coin"));
			
			
			if(targetCoord !=0)
				controller.updateCoins(originCoord, targetCoord, coin);
			else
				controller.updateCoins(originCoord, coin);
			
			
			Output out = new Output(response);
			out.format("ok");
				
		}
		break;
		

		case "changePassword": {
			
			Integer  uid =  Integer.parseInt(request.getParameter("userId"));
			String  newpass =  request.getParameter("newpass");
			controller.changePassword(uid, newpass);
			
			Output out = new Output(response);
			out.format("changed");
				
		}
		
		case "updateColor":{
			Integer  userId =  Integer.parseInt(request.getParameter("userId"));
			String  color =  request.getParameter("color");
			
			controller.updateColor(userId, color);
			Output out = new Output(response);	
			out.format("updated");
			
			
		}
		break;
		
		
		case "loseHexagon":{
			
			Integer  hexagonId =  Integer.parseInt(request.getParameter("hexagon"));
			controller.removeHexagon( hexagonId);
			Output out = new Output(response);
			out.format("deleted");
			
		}
		break;

		}
	}

}
	